#ifndef MATLIFEGAME_H
#define MATLIFEGAME_H
#include <stdlib.h>
#include "time.h"
#include <iostream>

using namespace std;

class MatLifeGame
{
    public:
        bool ** mat;
        int fil;
        int col;
        int r;
        MatLifeGame(int f,int c)
        {
            fil = f;
            col = c;
            mat = new bool * [fil];
            for(int i = 0;i < fil;i++)
            {
                mat[i] = new bool [col];
            }
        }
        void initialize()
        {
            for(int i=0;i < fil;i++)
            {
                r = rand()%col;
                for(int j=0;j < col;j++)
                {
                    if(j == r)
                        mat[i][j] = 1;
                    else
                        mat[i][j] = 0;
                }
            }
        }
        void print()
        {
            for(int i=0;i < fil;i++)
            {
                for(int j=0;j < col;j++)
                    cout << mat[i][j] << " ";
                cout << endl;
            }
            cout << endl;
        }

        int count_neighboors(int x, int y)
        {
            int c = 0;
            if(x - 1 >= 0 && y - 1 >= 0)
                if(mat[x - 1][y - 1] == 1)
                    c++;
            if(x - 1 >= 0)
                if(mat[x - 1][y] == 1)
                    c++;
            if(x - 1 >= 0 && y + 1 <= col - 1)
                if(mat[x - 1][y + 1] == 1)
                    c++;
            if(y - 1 >= 0)
                if(mat[x][y - 1] == 1)
                    c++;
            if(y + 1 <= col - 1)
                if(mat[x][y + 1] == 1)
                    c++;
            if(x + 1 <= fil - 1 && y - 1 >= 0)
                if(mat[x + 1][y - 1] == 1)
                    c++;
            if(x + 1 <= fil - 1)
                if(mat[x + 1][y] == 1)
                    c++;
            if(x + 1 <= fil - 1 && y + 1 <= col - 1)
                if(mat[x + 1][y + 1] == 1)
                    c++;
            return c;
        }



        void iterate(int x, int y)
        {
            int neigh = count_neighboors(x,y);
            if(mat[x][y]){
                if(neigh < 2)
                    mat[x][y] == 0;
                if(neigh > 3)
                    mat[x][y] == 0;
            }
            else{
                if(neigh == 3)
                    mat[x][y] == 1;
            }
        }

        void simulate()
        {
            for(int i = 0; i < fil; i++)
            {
                for(int j = 0; j < col; j++)
                {
                    iterate(i,j);
                }
            }
        }

        void exe(int n)
        {
            initialize();
            for(int i = 0; i < n; i++)
            {
                print();
                simulate();
            }
        }


        virtual ~MatLifeGame()
        {
            for(int i = 0;i < fil; i++)
                delete mat[i];
            delete [] mat;
        }
    protected:
    private:
};

#endif // MATLIFEGAME_H
